﻿using System;
using System.Text;
using System.Data;
using CRMFramework;

namespace CRMBiz
{
    public static class LastResultHandler
    {
        public static DataSet GetLastResultList()
        {
            DataSet ds;
            try
            {
                string strSql = "SELECT *,(select count(*) from crm_lastresult) as Total from crm_lastresult order by OrderNo";
                ds = DBhelper.ExecuteDataset(strSql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }

        public static DataSet GetLastResultList(int pageIndex, int pageSize)
        {
            DataSet ds;
            try
            {
                string sbSql = "SELECT *,(select count(*) from crm_lastresult) as Total from crm_lastresult order by OrderNo";
                ds = DBhelper.ExecuteDataset(sbSql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }

        public static int AddLastResult(string ResultDescription)
        {
            int intReturnValue = 0;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@ResultDescription", ResultDescription),
                };

                string sbSql = "Select * From crm_LastResult Where ResultDescription = @ResultDescription";
                DataSet ds = DBhelper.ExecuteDataset(sbSql, para);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    intReturnValue = 0;
                }
                else
                {
                    sbSql = "Insert into crm_lastresult values(0,'"+ResultDescription+"','')"+";";
                    string sbUpdate = "Update crm_lastresult set OrderNo=Last_Insert_ID() where ResultID=Last_Insert_ID()";
                    string finalQuery = sbSql + sbUpdate;
                    DBhelper.ExecuteNonQuery(finalQuery, para);
                    intReturnValue = 1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return intReturnValue;
        }

        public static int EditLastResult(int ResultID, string ResultDescription)
        {
            int intReturnValue = 0;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@ResultID", ResultID),
                    DBhelper.SetParameter("@ResultDescription", ResultDescription),
                };

                string sbSql = "Select * From crm_LastResult Where ResultID <> @ResultID And  ResultDescription = @ResultDescription";
                DataSet ds = DBhelper.ExecuteDataset(sbSql, para);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    intReturnValue = 0;
                }
                else
                {
                    sbSql = "update crm_LastResult set ResultDescription = @ResultDescription where ResultID = @ResultID";
                    DBhelper.ExecuteNonQuery(sbSql, para);
                    intReturnValue = 1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return intReturnValue;
        }

        public static bool DeleteLastResult(int ResultID)
        {
            bool flag = false;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@ResultID", ResultID),
                };

                string sbSql = "delete from crm_LastResult where ResultID=@ResultID";
                DBhelper.ExecuteNonQuery(sbSql, para);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public static int ReorderResult(int resultID, int orderNo)
        {
            int intReturnValue = 0;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@ResultID", resultID),
                    DBhelper.SetParameter("@OrderNo", orderNo),
                };

                string sbSql = "update crm_LastResult set OrderNo = @OrderNo where ResultID = @ResultID";
                DBhelper.ExecuteNonQuery(sbSql, para);
                intReturnValue = 1;
               
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return intReturnValue;
        }
    }
}
